﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dooshion.IDAL;
using System.Data;
using System.Data.SqlClient;
using Dooshion.Model;
using Dooshion.Log;
using Dooshion.Utility;

namespace Dooshion.DAL
{
    public class CP_RoleDAL : IBaseDAL<CP_Role>
    {
        Log.ILog logger = Log.LoggerManager.GetLogger(typeof(CP_RoleDAL).ToString());

        public int Insert(CP_Role t)
        {
            try
            {
                #region sql语句
                string sql = @"INSERT INTO [DooshionV4].[dbo].[CP_Role]
                                   ([RoleName1]
                                   ,[RoleDesc]
                                   ,[IsDelete])
                             VALUES
                                   (@RoleName
                                   ,@RoleDesc
                                   ,@IsDelete);
                           SELECT @IDENTITY = SCOPE_IDENTITY();
                           IF(@IDENTITY IS NULL)
                                SET @IDENTITY = 0;";
                #endregion
                int IDENTITY = 0;
                SqlParameter[] parameters = new SqlParameter[4];
                parameters[0] = new SqlParameter("@RoleName", t.RoleName);
                parameters[1] = new SqlParameter("@RoleDesc", t.RoleDesc);
                parameters[2] = new SqlParameter("@IsDelete", t.IsDelete);
                parameters[3] = new SqlParameter("@IDENTITY", IDENTITY);
                parameters[3].Direction = ParameterDirection.Output;
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters);
                IDENTITY = Convert.ToInt32(parameters[3].Value);
                return IDENTITY;
            }
            catch (Exception ex)
            {
                logger.Error("方法Insert中" + ex.Message.ToString());
                return 0;
            }
        }

        public bool Update(CP_Role t)
        {
            throw new NotImplementedException();
        }

        public bool Del(CP_Role t)
        {
            throw new NotImplementedException();
        }

        public CP_Role GetEntityById(int id)
        {
            try
            {
                List<CP_Role> list = GetList();
                CP_Role obj = list.FirstOrDefault(a => a.Id == id);
                return obj;
            }
            catch (Exception ex)
            {
                logger.Error("方法GetEntityById中" + ex.Message.ToString());
                return null;
            }
        }

        public List<CP_Role> GetEntityByCondition(CP_Role t, int pageSize, int pageIndex, out int rowCount)
        {
            try
            {
                List<CP_Role> list = GetList();
                if (t.Id > 0)
                {
                    list = list.Where(a => a.Id == t.Id).ToList<CP_Role>();
                }
                if (t.RoleName.Length > 0)
                {
                    list = list.Where(a => a.RoleName == t.RoleName).ToList<CP_Role>();
                }
                if (t.RoleDesc.Length > 0)
                {
                    list = list.Where(a => a.RoleDesc == t.RoleDesc).ToList<CP_Role>();
                }
                if (t.IsDelete != null)
                {
                    list = list.Where(a => a.IsDelete == t.IsDelete).ToList<CP_Role>();
                }
                rowCount = list.Count;
                list = list.Skip(pageIndex * pageSize).Take(pageSize).ToList<CP_Role>(); 
                return list;
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        public List<CP_Role> GetList()
        {
            if (DataCacheHelper.GetCache("CP_Role") == null)
            {
                string sql = @"SELECT [Id]
                                  ,[RoleName]
                                  ,[RoleDesc]
                                  ,[IsDelete]
                           FROM [DooshionV4].[dbo].[CP_Role]
                           WHERE IsDelete = 0";
                DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connStr, CommandType.Text, sql).Tables[0];
                List<CP_Role> list = new CreateEntity<CP_Role>().CreateEntityCollection<CP_Role>(dt);
                DataCacheHelper.SetCache("CP_Role", list, 30);
                return list;
            }
            else
            {
                return (List<CP_Role>)DataCacheHelper.GetCache("CP_Role");
            }
        }
    }
}
